﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Erebos
{
    public delegate void ScreenClickEvent(object sender, EventArgs e);

    public class Screen
    {
        private Dictionary<Rectangle, ScreenClickEvent> _clickEvents = new Dictionary<Rectangle, ScreenClickEvent>();

        public void AddEvent(Rectangle rectangle, ScreenClickEvent sce)
        {
            _clickEvents.Add(rectangle, sce);
        }

        private MouseState _oldMouseState = Mouse.GetState();

        public virtual void DrawBefore(GameTime gameTime) { }

        public virtual void Draw(SpriteBatch spriteBatch, GameTime gameTime) { }

        public virtual void Update(GameTime gameTime) 
        {
            if (Mouse.GetState().LeftButton == ButtonState.Released && _oldMouseState.LeftButton == ButtonState.Pressed)
            {
                foreach (KeyValuePair<Rectangle, ScreenClickEvent> kvp in _clickEvents)
                {
                    if (kvp.Key.Contains(Mouse.GetState().X, Mouse.GetState().Y))
                    {
                        kvp.Value(this, EventArgs.Empty);
                        break;
                    }
                }
            }

            _oldMouseState = Mouse.GetState();

            ClientHandler.Update(gameTime);
        }

        public void DrawStringCenter(SpriteBatch spriteBatch, SpriteFont font, string text, float y, Color color)
        {
            Vector2 textSize = font.MeasureString(text);
            Vector2 textPos = new Vector2((int)(ScreenSizeHelper.SizeWidth / 2 - textSize.X / 2), y);
            spriteBatch.DrawString(font, text, textPos, color);
        }

        public void CloseGame()
        {
            MainGame.Close = true;
        }
    }
}
